package com.nuoniu.sibanyun.controller.crm;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmCode;
import com.nuoniu.sibanyun.service.crm.ICrmBusinessService;
import com.nuoniu.sibanyun.service.crm.ICrmCodeService;
import com.nuoniu.sibanyun.service.crm.ICrmCustomerService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 编码设置 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-16
 */
@RestController
@RequestMapping("/crm/code")
@Api(tags = "编码设置")
public class CrmCodeController extends BaseController {

    @Autowired
    private ICrmCodeService crmCodeService;
    @Autowired
    private ICrmCustomerService ccustomerService;
    @Autowired
    private ICrmBusinessService businessService;

    /**
     * 获取编码列表
     * @author 小啊刚
     * @date 2020/6/16
     */
    @GetMapping("/list")
    public HttpResult getCodeList() {
        return HttpResult.ok(crmCodeService.getCodeList());
    }

    /**
     * 修改编码
     * @author 小啊刚
     * @date 2020/6/16
     */
    @GetMapping("/save")
    public HttpResult saveCode(CrmCode crmCode,  Boolean isUpdateAll) {
        crmCode.setCurrentCode(crmCode.getCurrentCode().toUpperCase());
        crmCodeService.updateById(crmCode);
        if(isUpdateAll) {
            if(crmCode.getDefaultCode().equals("C")) {//修改客户编码数据
                ccustomerService.updateCrmCode(crmCode.getCurrentCode());
            }
            if(crmCode.getDefaultCode().equals("SJ")) { //修改商机编码
                businessService.updateCode(crmCode.getCurrentCode());
            }
        }
        return HttpResult.ok();
    }
}
